/**
 * Copyright (C), 2001-2031, www.bosssof.com.cn
 * FileName: AbstractBusinessService.java
 * Author: chen
 * Date: 2024/6/2 14:23
 * Description:
 * <p>
 * History:
 * Date          Author   Version  Desc
 * 2024-01-01    bosssoft  1.0.0   initialize this file
 */
package com.bosssoft.g1.common.util.service;

import com.alibaba.fastjson.JSON;
import com.bosssoft.g1.common.annotation.page.MyPageInfo;
import com.bosssoft.g1.common.annotation.page.PaginationAspect;
import com.bosssoft.g1.common.constant.BaseContextHolder;

import com.bosssoft.g1.common.data.entity.BaseEntity;
import com.bosssoft.g1.common.data.entity.TenantBaseEntity;
import com.bosssoft.g1.common.data.vo.CommonPage;
import com.github.pagehelper.Page;
import java.util.Calendar;
import java.util.List;

/**
 * @className: AbstractBusinessService
 * @description:
 * @author: chen
 * @date: 2024/6/2 14:23
 * @since 1.0
 **/
public class AbstractBusinessService {
    public void baseDataStuff4Add(Object o) {
        //正常情况下系统的持久对象都是集成ＴenantBaseEntity即使是单租户系统也执行如此
        if (o instanceof TenantBaseEntity) {
            TenantBaseEntity baseEntity = (TenantBaseEntity) o;
            baseEntity.setCreator(BaseContextHolder.getLoginUserName());
            baseEntity.setCreatedBy(BaseContextHolder.getLoginUserId());
            baseEntity.setCreatedTime(Calendar.getInstance().getTime());
            baseEntity.setModifier(BaseContextHolder.getLoginUserName());
            baseEntity.setUpdatedBy(BaseContextHolder.getLoginUserId());
            baseEntity.setUpdatedTime(Calendar.getInstance().getTime());
            baseEntity.setModifier(BaseContextHolder.getLoginUserName());
            baseEntity.setTenantId(BaseContextHolder.getTanentId());
            baseEntity.setOrgId(BaseContextHolder.getOrgId());
            baseEntity.setCompanyId(BaseContextHolder.getCompanyId());
        } else if (o instanceof BaseEntity) {// 程序可能存在一些特殊的情况集成BaseEntity
            BaseEntity baseEntity = (BaseEntity) o;
            baseEntity.setCreatedBy(BaseContextHolder.getLoginUserId());
            baseEntity.setCreatedTime(Calendar.getInstance().getTime());
            baseEntity.setCreator(BaseContextHolder.getLoginUserName());
            baseEntity.setUpdatedBy(BaseContextHolder.getLoginUserId());
            baseEntity.setUpdatedTime(Calendar.getInstance().getTime());
            baseEntity.setModifier(BaseContextHolder.getLoginUserName());
        }

    }

    /**
     * 如果是修改者更新修改字段，至于版本号给mybatis插件自动维护，其他字段为业务类维护
     *
     * @param o 持久对象通过通过service方法传入
     */
    public void baseDataStuff4Updated(Object o) {
        if (o instanceof BaseEntity) {
            BaseEntity baseEntity = (BaseEntity) o;
            baseEntity.setModifier(BaseContextHolder.getLoginUserName());
            baseEntity.setUpdatedBy(BaseContextHolder.getLoginUserId());
            baseEntity.setUpdatedTime(Calendar.getInstance().getTime());
        }
    }


    public void baseDataStuffCommonPros(Object o) {

        if (o != null) {
            if (o instanceof TenantBaseEntity) {
                TenantBaseEntity baseEntity = (TenantBaseEntity) o;
                baseEntity.setTenantId(BaseContextHolder.getTanentId());
                baseEntity.setCompanyId(BaseContextHolder.getCompanyId());
                baseEntity.setOrgId(BaseContextHolder.getOrgId());
            } else if (o instanceof BaseEntity) {
                BaseEntity baseEntity = (BaseEntity) o;
                baseEntity.setCreatedBy(BaseContextHolder.getLoginUserId());
                baseEntity.setCreatedTime(Calendar.getInstance().getTime());
                baseEntity.setUpdatedBy(BaseContextHolder.getLoginUserId());
                baseEntity.setUpdatedTime(Calendar.getInstance().getTime());
            }
        }
    }

    protected CommonPage cast2CommonPage(Page page, List targetList) {
        return new CommonPage(page.getTotal(), page.getPageNum(), page.getPageSize(), page.getPages(), targetList);
    }

    /**
     * 某一些方法需要先将Page的信息保存到BaseContextHolder
     * abel.zhan 2023-05-30 created
     *
     * @param page mybatis 开始分页后查询返回的list
     */
    protected void setPageInfo2BaseContextHolder(Page page) {
        BaseContextHolder.set(PaginationAspect.PAGE_INFO, JSON.toJSONString(new MyPageInfo(page.getTotal(), page.getPages())));
    }

}